package cn.miju.admin.framework.response;

import cn.miju.admin.framework.content.CodeMessage;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import java.io.Serializable;

/**
 * <pre>
 * 接口响应参数类
 * </pre>
 *
 * @author zhuming
 * @version 1.0.0
 * @since 2023-03-01 14:55
 */
@JsonPropertyOrder({"code", "message", "data"})
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Result<T> implements Serializable {
    private static final long serialVersionUID = -4329433998677357451L;
    /**
     * 响应码
     */
    private Integer code;
    /**
     * 响应信息
     */
    private String message;
    /**
     * 响应参数
     */
    private T data;

    public static <T> Result<T> success() {
        return Result.success(null);
    }
    public static <T> Result<T> success(T data) {
        return Result.success(CodeMessage.SUCCESS.getMessage(), data);
    }
    public static <T> Result<T> success(String message, T data) {
        return new Result<>(CodeMessage.SUCCESS.getCode(), message, data);
    }

    public static <T> Result<T> fail(CodeMessage codeMessage) {
        return Result.fail(codeMessage.getCode(), codeMessage.getMessage());
    }

    public static <T> Result<T> fail(Integer code, String message) {
        return new Result<>(code, message);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Result(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }
}
